﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Data.Realm;

namespace Atlantyda
{
    class AimBot
    {
        public AimBot()
        {
        }

        public void StartAim()
        {
            bool go = true;
            while (Agent.Connected && go)
            {
                if (!Agent.locker)
                {
                    Agent.Look();
                    OrientedField[] energyFields = Agent.FindEnergy();
                    OrientedField beforeField = Agent.GetOrientateField(0, 2);
                    if (beforeField != null && beforeField.agent != null)
                        Agent.RotateLeft();    
                    else
                    {
                        beforeField = Agent.GetOrientateField(0, 1);
                        if (energyFields.Length > 0)
                        {
                            TreeRoads tr = new TreeRoads(new System.Drawing.Point(energyFields[0].x, energyFields[0].y));
                            tr.genarateCoordinates();
                            tr.genarateTree();
                            bool goal = tr.GoToEnergy();
                            if (goal)
                            {
                                if (Agent.AgentMap.Size == 0 && Agent.AgentMap.GetActualField().Energy == -1)
                                    checkSizeMap();
                                Agent.Recharge();
                            }
                        }
                        else if (beforeField.obstacle || beforeField.agent != null)
                        {
                            if (Agent.AgentMap.Fields.Count > 0)
                            {
                                AgentMapField actualt_field_archive = Agent.AgentMap.ActualFieldArchive;
                                AgentMapField actualt_field = Agent.AgentMap.GetActualField();
                                if (actualt_field_archive.LastOrientation == Agent.AgentMap.AgentOrientation)
                                {
                                    if (actualt_field_archive.LastRotate.Count > 0)
                                    {
                                        if (actualt_field_archive.LastRotate[0] == "right")
                                            Agent.RotateLeft();
                                        else
                                            Agent.RotateRight();
                                    }
                                }
                                else
                                {
                                    if (actualt_field.LastRotate.Count == 0 || actualt_field.LastRotate.Last() == "right")
                                        Agent.RotateRight();
                                    else
                                        Agent.RotateLeft();
                                }
                            }
                            else
                            {
                                Agent.RotateRight();
                                Agent.StepForward();
                            }
                        }
                        else
                        {
                            int code = Agent.StepForward();
                            if (code == 1)
                                go = false;
                            else if (code == 2)
                                Agent.RotateRight();
                        }
                    }
                }
            }
        }

        public void checkSizeMap()
        {
            foreach (KeyValuePair<string, AgentMapField> field in Agent.AgentMap.Fields)
            {
                if (field.Value.Energy == -1)
                {
                    if (field.Value.CoordianteX == Agent.AgentMap.AgentX
                        && field.Value.CoordianteY != Agent.AgentMap.AgentY)
                    {
                        int cy = field.Value.CoordianteY;
                        int ay = Agent.AgentMap.AgentY;
                        if (cy < 0)
                            cy = cy * (-1);
                        if (ay < 0)
                            ay = ay * (-1);
                        string size = (cy + ay + 1).ToString();
                        Agent.MainForm.mapSizeLabel.InvokeEx(mapSizeLabel => mapSizeLabel.Text = size);
                        Agent.AgentMap.Size = cy + ay + 1;
                    }
                    else if (field.Value.CoordianteX != Agent.AgentMap.AgentX
                        && field.Value.CoordianteY == Agent.AgentMap.AgentY)
                    {
                        int cx = field.Value.CoordianteX;
                        int ax = Agent.AgentMap.AgentX;
                        if (cx < 0)
                            cx = cx * (-1);
                        if (ax < 0)
                            ax = ax * (-1);
                        string size = (cx + ax + 1).ToString();
                        Agent.MainForm.mapSizeLabel.InvokeEx(mapSizeLabel => mapSizeLabel.Text = size);
                        Agent.AgentMap.Size = cx + ax + 1;
                    }
                }
            }
        }

        public void Speak()
        {

        }
    }
}
